    //
//  MovingAverageSetUp.m
//  iStock Global
//
//  Created by Andy on 2011/1/6.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "MovingAverageSetUp.h"
#import "MV_TableViewCell.h"
@implementation MovingAverageSetUp
@synthesize delegate;
@synthesize delegate_UIPopoverController;

#pragma mark -
#pragma mark Initialization

- (void)loadView {
	
	UIView *mainview;
	mainview=[[UIView alloc]initWithFrame:[UIScreen mainScreen].applicationFrame];
	self.view=mainview;
    [mainview release];
    
	self.view.backgroundColor=[UIColor lightTextColor];	
	UINavigationBar *nav;
	
	nav = [[UINavigationBar alloc] initWithFrame: CGRectMake(0.0f, 0.0f, 320.0f, 48.0f)];	
	
	UIBarButtonItem *barButton ;
	barButton = [[UIBarButtonItem alloc] initWithTitle:  NSLocalizedString(@"Done",@"完成")  style:UIBarButtonItemStylePlain target:self action:@selector(slideOut)];
	UINavigationItem *item;
	item = [[UINavigationItem alloc] initWithTitle:  NSLocalizedString(@"Moving Average",@"移動均線")  ];
	[item setRightBarButtonItem: barButton];
	[nav pushNavigationItem:item animated:NO];	
	[item release];
    
	mvarray=[[NSMutableArray alloc]init];
	[mvarray addObject:[NSLocalizedString(@"Moving Average",@"移動均線") stringByAppendingString: @":1"] ];
	[mvarray addObject:[NSLocalizedString(@"Moving Average",@"移動均線") stringByAppendingString: @":2"] ];
	[mvarray addObject:[NSLocalizedString(@"Moving Average",@"移動均線") stringByAppendingString: @":3"] ];
	[mvarray addObject:[NSLocalizedString(@"Moving Average",@"移動均線") stringByAppendingString: @":4"] ];	
	[mvarray addObject:[NSLocalizedString(@"Moving Average",@"移動均線") stringByAppendingString: @":5"] ];
	 	
	myTable = [[UITableView alloc] initWithFrame:CGRectMake(0.0f, 48.0f, 320.0f, self.view.frame.size.height)  style:UITableViewStyleGrouped];
    myTable.dataSource = self;	//連結資料 UITableViewDataSource
	myTable.delegate=self; //連結動作 UITableViewDelegate	
	[self.view addSubview:myTable];
	
	
	
	
	
	//[self.view addSubview:headerbutton];
	[mainview addSubview:nav ];	
	[nav release];
    
	pick=[[UIPickerView alloc ]initWithFrame:CGRectMake(0.0f, self.view.frame.size.height-216, 320.0f, 216.0f)];
	pick.delegate=self;
	pick.showsSelectionIndicator=YES;
	[pick reloadAllComponents];	
	
	NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
	
	[pick selectRow:[[prefs objectForKey:@"number_MVindicator1"] intValue] inComponent:0 animated:YES];		
	[pick selectRow:[[prefs objectForKey:@"number_MVindicator2"] intValue] inComponent:1 animated:YES];		
	[pick selectRow:[[prefs objectForKey:@"number_MVindicator3"] intValue] inComponent:2 animated:YES];		
	[pick selectRow:[[prefs objectForKey:@"number_MVindicator4"] intValue] inComponent:3 animated:YES];		
	[pick selectRow:[[prefs objectForKey:@"number_MVindicator5"] intValue] inComponent:4 animated:YES];		
	
	 
	 
	
	
	//pick.hidden=YES;
	[self.view addSubview:pick];	
	
	
	
	
}

#pragma mark -
#pragma mark  退出至上一層
-(void)slideOut{
		
	
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad){
        [delegate_UIPopoverController dismissPopoverAnimated:YES]; 
    }    
	else{
       [self.delegate MovingAverageSetUp_DidFinish:self];    
    }    
    
}

#pragma mark -
#pragma mark  表格事件

//區塊數量
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
	return 1;
}
//區塊與列數量
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
	return [mvarray count];
}
//表格內容
 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
	static NSString *CellIdentifier = @"MV_TableViewCell";
    
    MV_TableViewCell *cell =(MV_TableViewCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
	NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];	
	cell = [[[MV_TableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
	cell.code.text=[mvarray objectAtIndex:indexPath.row];
	 //顏色
	switch (indexPath.row) {
		case 0:
			cell.code.textColor=[UIColor redColor];
			break;
		case 1:
			cell.code.textColor=[UIColor lightGrayColor];
			break;	
		case 2:
			cell.code.textColor=[UIColor blueColor];
			break;
		case 3:
			cell.code.textColor=[UIColor greenColor];
			break;	
		case 4:
			cell.code.textColor=[UIColor orangeColor];
			break;
		 			
		default:
			break;
	}
	
	
	
	cell.name.text=[NSString stringWithFormat:@"%i",[[prefs objectForKey:[NSString stringWithFormat:@"number_MVindicator%i",indexPath.row+1] ] intValue]];
	//不讓開關點選時出現藍色
	cell.selectionStyle=UITableViewCellSelectionStyleNone;
	//開關
	UISwitch *onoff;
	onoff = [[UISwitch alloc] initWithFrame: CGRectZero];
	onoff.tag=indexPath.row+1;  
	[onoff addTarget: self action: @selector(switchChange:) forControlEvents: UIControlEventValueChanged];
		
	
	onoff.on=[[prefs objectForKey:[NSString stringWithFormat:@"isMVindicator%i" ,indexPath.row+1] ] boolValue];
	if (onoff.on==YES) {
		cell.name.textColor=[UIColor blueColor];
	}
	else {
		cell.name.textColor=[UIColor grayColor];
	}

	
	[self.view addSubview: onoff];
	cell.accessoryView=onoff;
	[onoff release];	
	
	
	return cell;	
	
}
 
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
	 
	
	
	
}

#pragma mark -
#pragma mark  按鈕轉換
-(void)switchChange:(id) sender{
	
	UISwitch *onoff = (UISwitch *) sender;	
	
	
	switch (onoff.tag) {
		case 1:{
			//搜尋是否要使用名稱
			NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
			[prefs setObject:[NSNumber numberWithBool: onoff.on]  forKey:@"isMVindicator1"];
			[prefs synchronize];	//將記憶體中的資料存到硬體上 儲存指令 			 
			
			break;
		}
		case 2:{
			//搜尋是否要使用名稱
			NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
			[prefs setObject:[NSNumber numberWithBool: onoff.on]  forKey:@"isMVindicator2"];
			[prefs synchronize];	//將記憶體中的資料存到硬體上 儲存指令 			 
			
			break;
		}
		case 3:{
			//搜尋是否要使用名稱
			NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
			[prefs setObject:[NSNumber numberWithBool: onoff.on]  forKey:@"isMVindicator3"];
			[prefs synchronize];	//將記憶體中的資料存到硬體上 儲存指令 			 
			
			break;
		}
		case 4:{
			//搜尋是否要使用名稱
			NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
			[prefs setObject:[NSNumber numberWithBool: onoff.on]  forKey:@"isMVindicator4"];
			[prefs synchronize];	//將記憶體中的資料存到硬體上 儲存指令 			 
			
			break;
		}
		case 5:{
			//搜尋是否要使用名稱
			NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
			[prefs setObject:[NSNumber numberWithBool: onoff.on]  forKey:@"isMVindicator5"];
			[prefs synchronize];	//將記憶體中的資料存到硬體上 儲存指令 			 
			
			break;
		}
			
			
	 			
		default:
			break;
	}
	
	
	[myTable reloadData];
	
	
}




#pragma mark -
#pragma mark  策略選擇器

// returns the number of 'columns' to display.
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
	return 5;
}

// returns the # of rows in each component..
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
	
	return 365;
	
	
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
	
	 return [NSString stringWithFormat:@"%i",row];
	
	
	
	
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
	if (row==0) {
		[pick selectRow:5 inComponent:component animated:YES];
	}
	else {
		
		NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
		[prefs setObject:[NSNumber numberWithInteger: row] forKey:[NSString stringWithFormat: @"number_MVindicator%i",component+1]];
		[prefs synchronize];	    
		[myTable reloadData];		
		
	}

}






- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc. that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}


@end
